<?php
/**
 *  单页内容控制器
 */
namespace app\admin\controller;
use app\admin\model\Single as SingleModel;
use app\admin\model\Extfield;
class Single extends Common
{
    private $model;

    private $blank;

    public function _initialize()
    {
		parent::_initialize();
        $this->model = new SingleModel();
    }

    // 单页内容列表
    public function index()
    {
		$UrlParam=input();
		$UrlParam['field']=input('field/s');
		$UrlParam['keyword']=input('keyword/s');
        if (! !(input('id')) && ($result=$this->model->getSingle(input('id')))) {
            $this->assign('more', true);
            $this->assign('content', $result);
        } else {
            $this->assign('list', true);
            if ($UrlParam['field'] && ($keyword = $UrlParam['keyword'])) {
                $result = $this->model->findSingle($field, $keyword);
            } else {
                $result = $this->model->getList();
            }
			$this->assign('btnqs', get_btn_qs());
			$this->assign('UrlParam',$UrlParam);
            $this->assign('contents', $result);
        }
		 return $this->fetch();
    }

    // 单页内容删除
    public function del()
    {
        if (! $id = get('id', 'int')) {
            error('传递的参数值错误！', - 1);
        }

        if ($this->model->delSingle($id)) {
            $this->log('删除单页内容' . $id . '成功！');
            success('删除成功！', - 1);
        } else {
            $this->log('删除单页内容' . $id . '失败！');
            error('删除失败！', - 1);
        }
    }

    // 单页内容修改
    public function mod()
    {
		$UrlParam=input();
        if (! $id = input('id/d')) {
            $this->error('传递的参数值错误！', - 1);
        }

        // 单独修改状态
        if (($field = input('field/s')) && ! is_null($value = input('value/d'))) {
			$result=array(
				"$field" => $value,
				'update_user' => session('username'),
			);
            if ($this->model->modSingle($id, $result)) {
                location(- 1);
            } else {
                alert('修改失败！','back');
            }
        }

        // 修改操作
		if($this->request->isPost()){
            // 获取数据
            $title = input('title');
            $content = input('content');
            $author = input('author');
            $source = input('source');
            $ico = input('ico'); 
            $pics = input('pics')?input('pics'):'';
            $titlecolor = input('titlecolor');
            $subtitle = input('subtitle');
            $outlink = input('outlink')?input('outlink'):'';
            $date = input('date');
            $enclosure = input('enclosure');
            $keywords = input('keywords');
            $description = input('description');
            $status = input('status/d');

            if (! $title) {
                alert('单页内容标题不能为空！','back');
            }

            // 自动提起前一百个字符为描述
            if (!$description && input('content')) {
                $description = msubstr(strip_tags(input('content')), 0, 150);
            }
            // 构建数据
            $data = array(
                'title' => $title,
                'content' => $content,
                'author' => $author,
                'source' => $source,
                'ico' => $ico,
                'pics' => $pics,
                'titlecolor' => $titlecolor,
                'subtitle' => $subtitle,
                'outlink' => $outlink,
                'date' => $date,
                'enclosure' => $enclosure,
                'keywords' => $keywords,
                'description' => $description,
                'status' => $status,
                'update_user' => session('username')
            );


            // 执行添加
            if ($this->model->modSingle($id, $data)) {
               $this->success('修改成功！', url('/Single/index/mcode/'.$UrlParam['mcode']));
            } else {
                location(- 1);
            }
        } else {
            // 调取修改内容
            if (! $result = $this->model->getSingle($id)) {
                $this->error('编辑的内容已经不存在！', url('/Single/index/mcode/'.$UrlParam['mcode']));
            }
            $this->assign('content', $result);

            // 扩展字段
            if (! $mcode = input('mcode')) {
                $this->error('传递的模型编码参数有误，请核对后重试！');
            }
			//$ExtFieldModel=new Extfield();
           // $this->assign('extfield', $ExtFieldModel->getModelField($mcode));
			$this->assign('UrlParam', $UrlParam);
            return $this->fetch();
        }
    }
}